package com.ykq.core.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * @program: aaa-hospital-parent
 * @description:
 * @author: 闫克起
 * @create: 2024-05-20 15:59
 **/
public class OssUtil {
    public static String uploadOss(MultipartFile myfile){
        // 自己的endpoint
        String endpoint = "oss-cn-qingdao.aliyuncs.com";
        //bucketNname的名称
        String bucketName = "aaa-qy173";
        // 填写Object完整路径，例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。
        String filename = UUID.randomUUID().toString().replace("-","")+myfile.getOriginalFilename();
        String objectName = filename; //上传到oss的名称

        String accessKeyId="LTAI5tMbCM6svgLqno22hW6N";
        String secretAccessKey="oIClnVEM2j6jpHikZuyIUcee3QALib";
        OSS ossClient = new OSSClientBuilder().build(endpoint,accessKeyId,secretAccessKey);
        try {
            ossClient.putObject(bucketName, objectName, myfile.getInputStream());
            //https://aaa-qy173.oss-cn-qingdao.aliyuncs.com/a.jpg
            return "https://"+bucketName+"."+endpoint+"/"+objectName; //放在数据库
        }  catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return null;
    }
}
